package com.luca.web.controller.system;

import com.luca.common.annotation.Log;
import com.luca.common.core.controller.BaseController;
import com.luca.common.core.domain.JsonResult;
import com.luca.common.core.page.TableDataInfo;
import com.luca.common.enums.BusinessType;
import com.luca.common.utils.poi.ExcelUtil;
import com.luca.system.domain.MfcBriefs;
import com.luca.system.service.IMfcBriefsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 蜜蜂查概要Controller
 *
 * @author luca
 * @date 2021-04-09
 */
@RestController
@RequestMapping("/system/briefs")
public class MfcBriefsController extends BaseController {

    private final IMfcBriefsService mfcBriefsService;

    @Autowired
    public MfcBriefsController(IMfcBriefsService mfcBriefsService) {
        this.mfcBriefsService = mfcBriefsService;
    }

    /**
     * 查询蜜蜂查概要列表
     */
    @PreAuthorize("@ss.hasPermi('system:briefs:list')")
    @GetMapping("/list")
    public TableDataInfo list(MfcBriefs mfcBriefs) {
        startPage();
        List<MfcBriefs> list = mfcBriefsService.selectMfcBriefsList(mfcBriefs);
        return getDataTable(list);
    }

    /**
     * 导出蜜蜂查概要列表
     */
    @PreAuthorize("@ss.hasPermi('system:briefs:export')")
    @Log(title = "蜜蜂查概要", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public JsonResult export(MfcBriefs mfcBriefs) {
        List<MfcBriefs> list = mfcBriefsService.selectMfcBriefsList(mfcBriefs);
        ExcelUtil<MfcBriefs> util = new ExcelUtil<>(MfcBriefs.class);
        return util.exportExcel(list, "briefs");
    }

    /**
     * 获取蜜蜂查概要详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:briefs:query')")
    @GetMapping(value = "/{id}")
    public JsonResult getInfo(@PathVariable("id") Long id) {
        return JsonResult.success(mfcBriefsService.selectMfcBriefsById(id));
    }

    /**
     * 新增蜜蜂查概要
     */
    @PreAuthorize("@ss.hasPermi('system:briefs:add')")
    @Log(title = "蜜蜂查概要", businessType = BusinessType.INSERT)
    @PostMapping
    public JsonResult add(@RequestBody MfcBriefs mfcBriefs) {
        mfcBriefsService.insertMfcBriefs(mfcBriefs);
        return JsonResult.success();
    }

    /**
     * 修改蜜蜂查概要
     */
    @PreAuthorize("@ss.hasPermi('system:briefs:edit')")
    @Log(title = "蜜蜂查概要", businessType = BusinessType.UPDATE)
    @PutMapping
    public JsonResult edit(@RequestBody MfcBriefs mfcBriefs) {
        mfcBriefsService.updateMfcBriefs(mfcBriefs);
        return JsonResult.success();
    }

    /**
     * 删除蜜蜂查概要
     */
    @PreAuthorize("@ss.hasPermi('system:briefs:remove')")
    @Log(title = "蜜蜂查概要", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public JsonResult remove(@PathVariable Long[] ids) {
        mfcBriefsService.deleteMfcBriefsByIds(ids);
        return JsonResult.success();
    }

    /**
     * 拉取新闻概要
     */
    public void pullBriefs() {
        String url = "https://data.block.cc/api/v3/briefs?locale=zh_CN&api_key=BQREAEZGLRGICGAJUQZWS0AIKWLUHTQ389GSOCF6";
        List<Map<String, Object>> data = new RestTemplate().getForObject(url, ArrayList.class);
        mfcBriefsService.pull(data);
    }

}
